package cn.net.msg.cxf.webservice.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import cn.net.msg.cxf.webservice.IMoodWebService;
import cn.net.msg.model.PageView;
import cn.net.msg.model.TbMood;
import cn.net.msg.service.TbMoodService;
import cn.net.msg.service.base.Order;
import cn.net.msg.utils.FilePathUtils;

@WebService
@SOAPBinding(style = Style.RPC)
@SuppressWarnings("deprecation")
public class MoodWebServiceImpl implements IMoodWebService {

    @Resource(name = "tbMoodService")
    private TbMoodService service;

    @Override
    public int addMood(String moodJson) {
        int code = 0;
        try{
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            TbMood tbMood = objectMapper.readValue(moodJson, TbMood.class);
            tbMood.setMoodCreattime(new Date());
            service.addMood(tbMood);
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.true"));
        } catch (JsonParseException e) {
            e.printStackTrace();
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.false"));
        } catch (JsonMappingException e) {
            e.printStackTrace();
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.false"));
        } catch (IOException e) {
            e.printStackTrace();
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.false"));
        } catch (Exception e) {
            e.printStackTrace();
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.false"));
        }
        return code;
    }

    @Override
    public List<TbMood> getMoodForList(int maxresult, int currentpage) {
        List<TbMood> list = new ArrayList<TbMood>();
        PageView<TbMood> pageView = new PageView<TbMood>(maxresult, currentpage);  

        list = service.getMoodForList(pageView.getFirstResult(),pageView.getMaxresult(), Order.DESC,"moodCreattime");

        return list;
    }

    @Override
    public int addMoodPraise(String moodJson) {
        int code = 0;
        try{
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            TbMood tbMood = objectMapper.readValue(moodJson, TbMood.class);
            service.addMoodPraise(tbMood);
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.true"));
        } catch (JsonParseException e) {
            e.printStackTrace();
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.false"));
        } catch (JsonMappingException e) {
            e.printStackTrace();
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.false"));
        } catch (IOException e) {
            e.printStackTrace();
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.false"));
        } catch (Exception e) {
            e.printStackTrace();
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.false"));
        }
        return code;
    }

    @Override
    public int addMoodBelittle(String moodJson) {
        int code = 0;
        try{
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            TbMood tbMood = objectMapper.readValue(moodJson, TbMood.class);
            service.addMoodBelittle(tbMood);
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.true"));
        } catch (JsonParseException e) {
            e.printStackTrace();
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.false"));
        } catch (JsonMappingException e) {
            e.printStackTrace();
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.false"));
        } catch (IOException e) {
            e.printStackTrace();
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.false"));
        } catch (Exception e) {
            e.printStackTrace();
            code = Integer.parseInt(FilePathUtils.readValue("cxfservice.return.code.false"));
        }
        return code;
    }

    @Override
    public List<TbMood> getUserMoodForList(long userid, int maxresult,
            int currentpage) {
        List<TbMood> list = new ArrayList<TbMood>();
        PageView<TbMood> pageView = new PageView<TbMood>(maxresult, currentpage);  
        List<HashMap<String,Object>> queryParams = new ArrayList<HashMap<String,Object>>();
        HashMap map = new HashMap();
        map.put("opera", "eq");
        map.put("field", "userId");
        map.put("value", userid);
        queryParams.add(map);

        list = service.getUserMoodForList(pageView.getFirstResult(),pageView.getMaxresult(), queryParams,  Order.DESC,"moodCreattime");

        return list;
    }
}
